package com.booklove.service.book;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.booklove.constant.DonateStateConstant;
import com.booklove.constant.JudgeStateConstant;
import com.booklove.context.BaseContext;
import com.booklove.domain.dto.DonatebookDTO;
import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.po.*;
import com.booklove.domain.query.DonatebookQuery;
import com.booklove.domain.vo.DonatebookVO;
import com.booklove.mapper.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 捐赠书信息 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-21
 */
@Service
public class DonatebookServiceImpl extends ServiceImpl<DonatebookMapper, Donatebook> implements DonatebookService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private BooksMapper booksMapper;

    @Autowired
    private DonatebookMapper donatebookMapper;

    @Autowired
    private DonatebookjudgeMapper donatebookjudgeMapper;


    @Override
    public PageDTO<DonatebookVO> queryDonatebooksPage(DonatebookQuery donatebookQuery) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则
        List<OrderItem> orderItems = donatebookQuery.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<Donatebook> page = donatebookQuery.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
        /*
        查询
         */
        Page<DonatebookVO> p = donatebookMapper.selectPageVO(page, donatebookQuery);

        // 获取数据信息
        return PageDTO.of(p, DonatebookVO.class);
    }

    @Override
    public DonatebookVO getVOById(Integer id) {
        return donatebookMapper.getVOById(id);
    }

    @Override
    public void createDonateBook(DonatebookDTO donatebookDTO) {
        // 验证信息非空
        /*
        - 捐赠title
        - 捐赠发言recommend
        - 捐赠书籍名称name
         */
        //TODO 打开基本信息验证
//        if (donatebookDTO.getTitle() == null || donatebookDTO.getTitle().isBlank()
//                || donatebookDTO.getName() == null || donatebookDTO.getName().isBlank()
//                || donatebookDTO.getRecommend() == null || donatebookDTO.getRecommend().isBlank()
////                || donatebookDTO.getCover() == null || donatebookDTO.getCover().isBlank()// 封面不强作为要求
//        ) {
//            throw new DonateBookCreateException("书籍信息或是捐献信息缺失");
//        }
//
//        Integer addressId = donatebookDTO.getAddressId();
//        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(addressId != null, Address::getId, addressId);
//        Address address = addressMapper.selectOne(wrapper);
//        if (address == null || address.getUserId() != BaseContext.getCurrentId().intValue()) {
//            throw new AddressSubmitException("地址信息非法，请检测内容");
//        }


        // 插入书本
        Books book = new Books();
        BeanUtils.copyProperties(donatebookDTO, book);
        booksMapper.insert(book);
        Integer bookId = book.getId();
        // 创建捐献
        Donatebook donatebook = this.createDonatebook(donatebookDTO);
        // 回填书本id
        donatebook.setBookId(bookId);
        // 用户id
        donatebook.setUserId(BaseContext.getCurrentId().intValue());

        // 创建审核项目
        Donatebookjudge donatebookjudge = new Donatebookjudge();
        BeanUtils.copyProperties(donatebook, donatebookjudge);

        // 保存
        donatebookjudgeMapper.insert(donatebookjudge);
    }


    /**
     * @description 创建一个捐赠书本实体
     * @author zken
     * @CreateDate 2024/10/19 18:18:12
     */
    public Donatebook createDonatebook(DonatebookDTO donatebookDTO) {

        // 创建捐献
        Donatebook donatebook = new Donatebook();
        BeanUtils.copyProperties(donatebookDTO, donatebook);
        // 用户id
        donatebook.setUserId(BaseContext.getCurrentId().intValue());

        // 将捐献的书籍放置进入待审核状态
        donatebook.setJudgeState(JudgeStateConstant.WAITING_JUDGE);

        // 将捐献的书籍变为等待领取
        donatebook.setDonateState(DonateStateConstant.WAITING);

        // 设置donate时间
        donatebook.setDonateTime(LocalDateTime.now());
        donatebook.setCreatedBy(BaseContext.getCurrentId().intValue());
        donatebook.setCreatedTime(LocalDateTime.now());
        donatebook.setUpdatedBy(BaseContext.getCurrentId().intValue());
        donatebook.setUpdatedTime(LocalDateTime.now());

        return donatebook;
    }
}
